package juc.thread;

import java.util.concurrent.Callable;

/*
* 两阶段终止
* */
public class Test2 {
    public static void main(String[] args) throws InterruptedException {
        TwoPhaseStop.start();
        Thread.sleep(6500);
        TwoPhaseStop.stop();
    }
}
class TwoPhaseStop{
    private   static Thread monitor=null;
    public static void start(){
           monitor=new Thread(()->{
               while (true){
                   if(monitor.isInterrupted())return;
                   System.out.println("监控内存...");
                   try {
                       Thread.sleep(2000);
                   } catch (InterruptedException e) {
                       monitor.interrupt();//在线程睡眠过程中被打断 会清除打断记录 所以需要重新标记
                       e.printStackTrace();
                   }
               }
           },"monitorMemory");
           monitor.start();
    }
    public static void stop(){
        monitor.interrupt();
    }
}
